<?php
/*
	[gunianwang system]
	NAME:jobs_control
	AUTHOR:hl
	DATE:2012-07-02
*/
if (!defined('IN_ELS'))
{
    die('Hacking attempt');
}
class control extends jobsbase {
	var $pagecount 		= 0;
	var $currendnum 	= 1;
	var $pageSize 		= 15;	//每页多少条
	var $pageNumSize 	= 10;	//分页数
	
	function control() {
		$this->jobsbase();
		if(!empty($_REQUEST['currendnum'])){
			$this->currendnum=$_REQUEST['currendnum'];
		}
		$act=empty($_REQUEST['a']) ? 'empty' : $_REQUEST['a'];
		
		if($act=="empty")				$this->jobsList();
		if($act=="jobsadd")				$this->jobsEdit();
		if($act=="jobsedit")			$this->jobsEdit();
		if($act=="jobssave")			$this->jobsSave();
		if($act=="jobsdel") 			$this->jobsDel();
		if($act=="jobsdelall") 			$this->jobsDelall();
	}


	function jobsList(){
		$where = "where 1=1";
		$url = "";
		$dataCount=$this->getjobsCount($where);
		$startNum = $this->pageSize*($this->currendnum-1);
		$limitText = " limit ".$startNum." , ".$this->pageSize;
		$pagecountdata=array();
		require_once ELS_ROOT.'lib/paging.class.php';
		$pg =  new paging();
		$pg -> setLinkText("<a href='admin.php?p=jobs$url&currendnum=!!PageNum!!'>!!LinkText!!</a>");
		$pg -> setCurrendnum(  $this-> currendnum);
		$pg -> setPageSize(    $this-> pageSize);
		$pg -> setPageNumSize( $this-> pageNumSize);
		$pg -> setDataCount(   $dataCount);
		$pagecountdata = $pg ->makePageText();
		
		$list=$this->getjobsList($where,$limitText);
		$this->view->assign('list',$list);
		$this->view->assign('pagecount' ,$pagecountdata);
		$this->view->assign('currendnum',$currendnum);
		
		$this->view->display("admin_jobslist");
	}
	//合作机构添加修改初始页面
	function jobsEdit(){
		if($_REQUEST['t']=='modify'){
			$jobs_id  = $_REQUEST['jobs_id'];
			$data=$this->getjobsContent($jobs_id);
			$this->view->assign('data' ,$data);
			$this->view->assign('active_type' ,'modify');
		}else{
			$data[is_ok] = 1; 
			$data[is_tuijian] = 1; 
			$this->view->assign('data' ,$data);
			$this->view->assign('active_type' ,'add');
		}
		$this->view->display("admin_jobsedit");
	}
	//合作机构添加入库操作
	function jobsSave(){
		$active_type	= $_REQUEST['active_type'];
		$name   		= $_REQUEST["name"];
		$source 		= $_REQUEST["source"];
		$s_time 		= $_REQUEST["s_time"];
		$e_time 		= $_REQUEST["e_time"];
		$content 		= $_REQUEST["content"];
		$company_content = $_REQUEST["company_content"];
		$is_ok 			= empty($_REQUEST['is_ok'])?0:intval($_REQUEST['is_ok']);
		$is_tuijian 	= empty($_REQUEST['is_tuijian'])?0:intval($_REQUEST['is_tuijian']);
		$path 			= $_FILES["s_img"]['name'];
		$path_b 		= $_FILES["b_img"]['name'];
		
		$hasError = false;
		if($active_type == "add"){
			if($path == "" ){
				$this->view->assign('path_err'  ,"请上传图片");
				$hasError = true;
			}
			if($path_b == "" ){
				$this->view->assign('path_err'  ,"请上传图片");
				$hasError = true;
			}
		}
		
		$name1 = gmdate('Ym');
		$name2 = gmdate('j');
		//创建稿件文件夹
		$file_path = 'data/upImage/'.$name1;
		if(!@file_exists($file_path)){
			@mkdir($file_path,   0777);
		}
		$file_path_dir = 'data/upImage/'.$name1.'/'.$name2.'/';
		if(!@file_exists($file_path_dir)){ 
			@mkdir($file_path_dir,   0777);
		}
		$file_err = 'path_err';
		$picture_name = $this->uploadImage($path,$_FILES['s_img'],$file_path_dir,'paths_err');
		$picture_name1 = $this->uploadImage($path_b,$_FILES['b_img'],$file_path_dir,'paths_err');
		
		//有错误
		if($picture_name == 1||$picture_name1 == 1){
			$this->view->assign('path_err'  ,"图片上传失败！");
			$hasError = true;
		}
		
		$dataArray=array();
		$dataArray['name'] 					= trim($name);
		$dataArray['source'] 				= trim($source);
		$dataArray['is_tuijian'] 			= $is_tuijian;
		$dataArray['is_ok'] 				= $is_ok;
		$dataArray['s_time'] 				= $s_time;
		$dataArray['e_time'] 				= $e_time;
		$dataArray['content'] 				= $this->replaceHtmlToAdditional(trim($content));
		$dataArray['company_content'] 		= $this->replaceHtmlToAdditional(trim($company_content));
		
		if($name == "" ){
			$this->view->assign('errname'  ,"名称不能为空");
			$hasError = true;
		}
		if($s_time == "" || $e_time == ''){
			$this->view->assign('errtime'  ,"时间不能为空");
			$hasError = true;
		}
		//如果有错误,提示错误消息并结束
		if($hasError){
			//如果已上传图片删除掉
			if($picture_name != ""){
				$this->deleteFile(ELS_ROOT.$file_path_dir.$picture_name);
			}
			if($picture_name1 != ""){
				$this->deleteFile(ELS_ROOT.$file_path_dir.$picture_name1);
			}
			//如果是修改信息
			if($active_type == "modify"){
				$jobs_id 		= 	$_REQUEST['jobs_id'];
				$data=$this->getjobsContent($jobs_id);
				$dataArray = $data;
			}
			
			$this->view->assign('active_type' ,$active_type);
			$this->view->assign('data' ,$dataArray);
			$this->view->display("admin_jobsedit");
			exit;
		}
		//添加信息状态
		if($_REQUEST['active_type']=='add'){
			if($s_time !='' &&$e_time != ''){
				$dataArray['s_time'] 				= strtotime($s_time." 00:00:00")-8*3600;
				$dataArray['e_time'] 				= strtotime($e_time." 23:59:59")-8*3600;
			}
			$dataArray['s_img']			= $name1.'/'.$name2.'/'.$picture_name;	//图片路径入库
			$dataArray['b_img']			= $name1.'/'.$name2.'/'.$picture_name1;	//图片路径入库
			$dataArray['uid'] 			= $_SESSION["admin"]['uid'];//操作人
			$dataArray['last_time'] 	= $this->getCurrTimeLine();//最后时间
			$dataArray['create_time'] 	= $this->getCurrTimeLine();//创建时间
			$this->insertjobs($dataArray);
		}else{//修改信息状态
			$jobs_id = empty($_REQUEST['jobs_id'])?0:$_REQUEST['jobs_id'];
			//根据id查询信息
			$data = $this->getjobsContent($jobs_id);
			
			//如果上传图片判断以前有没有上传过图片，有就更新掉；
			if(!empty($path)){
				if(!empty($data["s_img"])){
					$this->deleteFile(ELS_ROOT.'./data/upImage/'.$data["s_img"]);//删除图片
				}
				$dataArray['s_img'] = $name1.'/'.$name2.'/'.$picture_name;
			}
			
			if(!empty($path_b)){
				if(!empty($data["b_img"])){
					$this->deleteFile(ELS_ROOT.'./data/upImage/'.$data["b_img"]);//删除图片
				}
				$dataArray['b_img'] = $name1.'/'.$name2.'/'.$picture_name1;
			}
			if($s_time !='' && $e_time != ''){
				$dataArray['s_time'] 				= strtotime($s_time." 00:00:00")-8*3600;
				$dataArray['e_time'] 				= strtotime($e_time." 23:59:59")-8*3600;
			}
			$dataArray['uid'] 			= $_SESSION["admin"]['uid'];//操作人
			$dataArray['last_time'] 	= $this->getCurrTimeLine();//最后时间
			$where ="jobs_id=".$jobs_id;
			$this->updatejobs($dataArray,$where);
		}
		$this->pageRedirect("admin.php?p=jobs");
	
	}
	//合作机构删除操作
	function jobsDel(){
		$jobs_id = empty($_REQUEST['jobs_id'])?0:$_REQUEST['jobs_id'];
		
		$data = $this->getjobsContent($jobs_id);
		if(!empty($data["s_img"])){
			$this->deleteFile(ELS_ROOT.'data/upImage/'.$data["s_img"]);
		}
		if(!empty($data["b_img"])){
			$this->deleteFile(ELS_ROOT.'data/upImage/'.$data["b_img"]);
		}
		$this->db->query("DELETE FROM gnw_jobs WHERE jobs_id = '$jobs_id'");
		
		$this->pageRedirect("?p=jobs");
	}
	//合作机构批量删除
	function jobsDelall(){
		$jobs_id= empty($_POST['ids'])?array():$_POST['ids'];
		//删除
		if($jobs_id){
			for($i=0;$i<count($jobs_id);$i++){
				$data[$i] = $this->getjobsContent($jobs_id[$i]);
				if(!empty($data[$i]["s_img"])){
					$this->deleteFile(ELS_ROOT.'data/upImage/'.$data[$i]["s_img"]);
				}
				if(!empty($data[$i]["b_img"])){
					$this->deleteFile(ELS_ROOT.'data/upImage/'.$data[$i]["b_img"]);
				}
				$this->db->query("DELETE FROM gnw_jobs WHERE jobs_id = '$jobs_id[$i]'");
			}
		}
		
		$this->pageRedirect("?p=jobs");
	}
}
?>